using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.InputSystem;

namespace SunshineFarmWord
{
    public class PlayerAxeState : PlayerBaseStates
    {
        // 需要砍树的次数
        private int _cutsTreeNumber = 0;
        //砍的树
        private GameObject _axeTree;
        public PlayerAxeState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
        {
            if (stateMachine.ReusableData.PlayerStatesData.PlayerAxeData == null) stateMachine.ReusableData.PlayerStatesData.PlayerAxeData = new PlayerAxeData();
        }

        #region Base function

        public override void Enter()
        {
            base.Enter();

            _axeTree = stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.AxeTreeNode;

            InitReusableData();

            //判断树的方向和人的方向
            ChangeFaceDirection(_axeTree);

            StartAnimation(stateMachine.Player.AnimationData.AxeParameterHash);
        }

        public override void Exit()
        {
            base.Exit();

            StopAnimation(stateMachine.Player.AnimationData.AxeParameterHash);
        }
        #endregion


        #region Main function

        //初始化参数
        private void InitReusableData()
        {
            stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.AxeTreeCount = 0;
            //获取随机砍树次数
            TreeScript treeScript = _axeTree.GetComponent<TreeScript>();
            _cutsTreeNumber = treeScript.GenerateCutsTreeNumber(playerStatesData.PlayerAxeData.CutsTreeMinNumber, playerStatesData.PlayerAxeData.CutsTreeMaxNumber);

            treeScript.SetResetTreeTime(playerStatesData.PlayerAxeData.ResetTreeTime);
            //有定时任务取消掉
            treeScript.CancelAutoResetTree();
            //stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.CutsTreeMinNumber = playerStatesData.PlayerAxeData.CutsTreeMinNumber;
            //stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.CutsTreeMaxNumber = playerStatesData.PlayerAxeData.CutsTreeMaxNumber;
            //_cutsTreeNumber = Random.Range(stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.CutsTreeMinNumber, stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.CutsTreeMaxNumber);

        }

        // 切换为空闲状态
        private void ChangeStateToIdle()
        {
            stateMachine.ChangeState(stateMachine.IdlingState);
        }

        // 砍树计算
        private void CalculateAxeTreeTime()
        {
            stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.AxeTreeCount += 1;
            if (stateMachine.ReusableData.PlayerStatesData.PlayerAxeData.AxeTreeCount >= _cutsTreeNumber)
            {
                //砍完了
                ChangeStateToIdle();
            }
        }
        #endregion


        #region 动画监听

        public override void OnAnimationTransitionEvent()
        {
            base.OnAnimationTransitionEvent();

            CalculateAxeTreeTime();

            if (_axeTree != null)
            {
                TreeScript treeScript = _axeTree.GetComponent<TreeScript>();
                // 计算掉落物掉落的方向(有点问题...)
                Vector2 n = (_axeTree.transform.position - stateMachine.Player.transform.position).normalized;
                //砍树
                treeScript.CuttingDownTrees(n);
                //执行树摇动
                treeScript.Shake(0.5f);
            }
        }
        #endregion


        #region 按键监听

        // 交互按键
        protected override void OnInteractStarted(InputAction.CallbackContext context)
        {
            base.OnInteractStarted(context);

            //树进行自动恢复
            if (_axeTree != null) _axeTree.GetComponent<TreeScript>().StartAutoResetTree();

            // 砍树状态下 按下交互 表示 停止砍树
            stateMachine.ChangeState(stateMachine.IdlingState);
        }
        #endregion
    }
}
